<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/3
 * Time: 17:32
 */

namespace app\home\model;

use think\Model;

class Cases extends Model
{
    //
    public function search($cid)
    {
        // 搜索所有的数据,如果需要搜索其他字段需要自己添加
        $where = 1;
        if($cid)
        {
            $where .= " and id in (SELECT case_id from tr_case_middle WHERE case_cat_id = '{$cid}')";
        }
        /** 翻页 **********/
        //1 . 算出总的记录数
        $count = $this->where($where)->count();
        // 2. 生成翻页类的对象
        $page = input('page')?:1;
        $page = new \Component\HomePage($count,6,$page);
        $page->config['header'] = '个案例';
        // 3. 生成翻页的字符串：上一页、下一页
        $pageStr = $page->fpage();
        // 4. 取出当前页的数据
        $data = db('cases')->where($where)->limit( $page->limit)->order("id desc")->select();
        return array(
            'page' => $pageStr,
            'cases' => $data,
        );
    }


    /**
     * 构造含有child下标的数组
     * @param $cate
     * @param string $name
     * @param int $pid
     * @return array
     */
    public function getChildArr($cate, $name = 'child', $pid = 0)
    {
        $arr = array();
        foreach($cate as $v)
        {
            if($v['parent_id'] == $pid)
            {
                $v[$name] = $this->getChildArr($cate, $name, $v['cat_id']);
                $arr[] = $v;
            }
        }
        return $arr;
    }

}